package com.k.kpp.util;

import org.apache.commons.net.ftp.FTPClient;

import java.io.*;

public class FtpUtil {
    public static void main(String[] args) {
        try {
            FTPClient ftpClient = getFtpClient();
            String host = "127.0.0.1";
            String username = "admin";
            String password = "123456";
            int port = 21;
            boolean login = login(ftpClient, host, username, password, port);
            System.out.println("登录 FTP:" + login);

            // 创建目录
            String myDir = "/haha11/aa11";// OK
            boolean changeDirOk = ftpClient.changeWorkingDirectory(myDir);
            System.out.println("切换目录" + changeDirOk);
            if (!changeDirOk) {
                System.out.println("切换目录失败,创建目录");
                mkdir(ftpClient, myDir);
                boolean changeDirCreateOk = ftpClient.changeWorkingDirectory(myDir);
                System.out.println("切换目录" + changeDirCreateOk);
            }

            // 上传文件
//            FileInputStream fileInputStream = new FileInputStream("e:/test/b.xlsx");
//            boolean uploadFileFlag = uploadFile(ftpClient,"/a/b/c","好当家.xlsx",fileInputStream);
//            System.out.println("文件上传返回："+uploadFileFlag);
//
//            FTPFile[] files = ftpClient.listFiles("/voice1/aaa");
//            for (FTPFile f : files) {
//                System.out.println(f.getName());
//            }
//
//            // 下载文件
//            boolean downloadFileFlag = downloadFile(ftpClient, "/voice1", "加班乘车表格-姜德明.xlsx", "e:/upload", "xx.xlsx");
//            System.out.println("文件下载结果："+downloadFileFlag);

            boolean delFlag = deleteFile(ftpClient,"/a/b/c","好当家.xlsx");
            System.out.println("删除文件:好当家.xlsx "+delFlag);

            // 登出
            logout(ftpClient);
            System.out.println("登出 FTP");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static FTPClient getFtpClient() {
        return new FTPClient();
    }

    public static boolean login(FTPClient ftpClient, String host, String username, String password, int port) throws IOException {
        ftpClient.setControlEncoding("utf-8");
        ftpClient.connect(host, port);
        return ftpClient.login(username, password);
    }

    public static void mkdir(FTPClient ftpClient, String dir) throws IOException {
        ftpClient.makeDirectory(dir);
    }
    public static boolean changeDir(FTPClient ftpClient, String dir) throws IOException {
        return ftpClient.changeWorkingDirectory(dir);
    }

    public static void logout(FTPClient ftpClient) throws IOException {
        ftpClient.logout();
        ftpClient.disconnect();
    }

    public static boolean uploadFile(FTPClient ftpClient, String saveName, InputStream is) throws IOException {
        return ftpClient.storeFile(saveName, is);
    }

    public static boolean uploadFile(FTPClient ftpClient, String uploadDir, String uploadName, InputStream is) throws IOException {
        boolean cdFlag = ftpClient.changeWorkingDirectory(uploadDir);
        if (!cdFlag) {
            mkdir(ftpClient, uploadDir);
            ftpClient.changeWorkingDirectory(uploadDir);
        }
        return uploadFile(ftpClient, uploadName, is);
    }

    public static boolean uploadFile(FTPClient ftpClient, String uploadDir, String uploadName, String localDir, String localName) throws IOException {
        return uploadFile(ftpClient, uploadDir, uploadName, new FileInputStream(localDir + localName));
    }

    public static boolean downloadFile(FTPClient ftpClient, String fileDir, String fileName, OutputStream outputStream) throws IOException {
        boolean cdFlag = ftpClient.changeWorkingDirectory(fileDir);
        if (!cdFlag) {
            throw new RuntimeException("文件目录不存在");
        }
        return ftpClient.retrieveFile(fileName, outputStream);
    }

    public static boolean downloadFile(FTPClient ftpClient, String fileDir, String fileName, String downloadDir, String downloadName) throws IOException {
        return downloadFile(ftpClient, fileDir, fileName, new FileOutputStream(downloadDir + "/" + downloadName));
    }

    public static boolean deleteFile(FTPClient ftpClient,String fileDir, String fileName) throws IOException {
        boolean changeDir = changeDir(ftpClient,fileDir);
        if(!changeDir){
            throw new RuntimeException("目录不存在");
        }
        return ftpClient.deleteFile(fileName);
    }
}
